package com.pdg.android.splorr.data;

import com.pdg.android.splorr.R;
import com.pdg.android.splorr.data.baseclasses.DataClient;
import com.pdg.android.splorr.data.enums.DoorState;
import com.pdg.android.splorr.data.enums.LockDetail;
import com.pdg.android.splorr.data.enums.LockMaterial;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Door extends DataClient<Room> {
	
	private static final String KEY_PREFIX_FORMAT = "%1$s%2$s_";

	private static final String SUBKEY_LOCK_MATERIAL = "lock_material";
	private static final String SUBKEY_LOCK_DETAIL = "lock_detail";
	private static final String SUBKEY_TRANSITION = "transition";

	private static final DoorState DEFAULT_TRANSITION = DoorState.WALL;
	private static final LockMaterial DEFAULT_LOCK_MATERIAL = LockMaterial.NONE;
	private static final LockDetail DEFAULT_LOCK_DETAIL = LockDetail.NONE;

	private static final String DEFAULT_WALL_MARK = "";

	private static final String SUBKEY_WALL_MARK = "wall_mark";

	private DoorState doorState = DEFAULT_TRANSITION;
	private LockMaterial lockMaterial = DEFAULT_LOCK_MATERIAL;
	private LockDetail lockDetail = DEFAULT_LOCK_DETAIL;
	private String wallMark = DEFAULT_WALL_MARK;

	public Door(Room theData) {
		super(theData);
	}

	@Override
	protected void onLoad(SharedPreferences thePreferences) {
		setDoorState(DoorState.valueOf(thePreferences.getString(getSubKey(SUBKEY_TRANSITION), DEFAULT_TRANSITION.toString())));
		setLockMaterial(LockMaterial.valueOf(thePreferences.getString(getSubKey(SUBKEY_LOCK_MATERIAL), DEFAULT_LOCK_MATERIAL.toString())));
		setLockDetail(LockDetail.valueOf(thePreferences.getString(getSubKey(SUBKEY_LOCK_DETAIL), DEFAULT_LOCK_DETAIL.toString())));
		setWallMark(thePreferences.getString(getSubKey(SUBKEY_WALL_MARK),DEFAULT_WALL_MARK));
	}

	@Override
	protected void onCommit(Editor theEditor) {
		theEditor.putString(getSubKey(SUBKEY_TRANSITION), getDoorState().name());
		theEditor.putString(getSubKey(SUBKEY_LOCK_MATERIAL), getLockMaterial().name());
		theEditor.putString(getSubKey(SUBKEY_LOCK_DETAIL), getLockDetail().name());
		theEditor.putString(getSubKey(SUBKEY_WALL_MARK), getWallMark());
	}

	private String getKeyPrefix() {
		return String.format(KEY_PREFIX_FORMAT, getData().getKeyPrefix(),getData().getDoorDirection(this).name());
	}
	
	private String getSubKey(String theSubKey){
		return getKeyPrefix()+theSubKey;
	}

	@Override
	public Context getContext() {
		return getData().getContext();
	}

	public DoorState getDoorState() {
		return doorState;
	}

	public void setDoorState(DoorState transition) {
		this.doorState = transition;
	}

	public LockMaterial getLockMaterial() {
		return lockMaterial;
	}

	public void setLockMaterial(LockMaterial lockMaterial) {
		this.lockMaterial = lockMaterial;
	}

	public LockDetail getLockDetail() {
		return lockDetail;
	}

	public void setLockDetail(LockDetail lockDetail) {
		this.lockDetail = lockDetail;
	}

	public String getWallMark() {
		return wallMark;
	}

	public void setWallMark(String wallMark) {
		this.wallMark = wallMark;
	}
	
	public boolean hasWallMark(){
		return getWallMark().length()>0;
	}
	
	public String getLockName(){
		return String.format(getContext().getString(R.string.lock_name_format),
				getContext().getResources().getStringArray(getLockMaterial().getStringArrayId())[getLockMaterial().getArrayIndex()],
				getContext().getResources().getStringArray(getLockDetail().getStringArrayId())[getLockDetail().getArrayIndex()]
				);
	}

}
